package com.cqvip.innocence.project.model.dto;


import java.util.HashMap;
import java.util.List;

/**
 * @ClassName JsonResult
 * @Description 定义统一的responseDto
 * @Author Innocence
 * @Date 2020/7/14 10:03
 * @Version 1.0
 */
public class JsonResult extends HashMap<String, Object> {
    private static final long serialVersionUID = 1628608837693261312L;

    /**
     * 操作成功
     */
    public static final Integer SUCCESS = 200;

    /**
     * 对象创建成功
     */
    public static final Integer CREATED = 201;

    /**
     * 请求已经被接受
     */
    public static final Integer ACCEPTED = 202;

    /**
     * 操作已经执行成功，但是没有返回数据
     */
    public static final Integer NO_CONTENT = 204;

    /**
     * 资源已被移除
     */
    public static final Integer MOVED_PERM = 301;

    /**
     * 重定向
     */
    public static final Integer SEE_OTHER = 303;

    /**
     * 资源没有被修改
     */
    public static final Integer NOT_MODIFIED = 304;

    /**
     * 参数列表错误（缺少，格式不匹配）
     */
    public static final Integer BAD_REQUEST = 400;

    /**
     * 未授权
     */
    public static final Integer UNAUTHORIZED = 401;
    public static final String UNAUTHORIZED_MSG = "unauthorized";

    /**
     * 访问受限，授权过期,无权限
     */
    public static final Integer FORBIDDEN = 403;
    public static final String FORBIDDEN_MSG = "forbidden";

    /**
     * 资源，服务未找到
     */
    public static final Integer NOT_FOUND = 404;

    /**
     * 不允许的http方法
     */
    public static final Integer BAD_METHOD = 405;

    /**
     * 资源冲突，或者资源被锁
     */
    public static final Integer CONFLICT = 409;

    /**
     * 不支持的数据，媒体类型
     */
    public static final Integer UNSUPPORTED_TYPE = 415;

    /**
     * 系统内部错误
     */
    public static final Integer ERROR = 500;

    /**
     * 接口未实现
     */
    public static final Integer NOT_IMPLEMENTED = 501;

    public static final String CODE_TIMEOUT = "timeout";

    /**
     * 敏感词错误
     */
    public static final String SENSITIVE_ERROR = "sensitive_error";

    /**
     * 敏感词错误码
     */
    public static final Integer ERROR_SENSITIVE_CODE = 618;

    /**
     * 字段超出数据库设置长度
     */
    public static final Integer ERROR_SQL_TOOLONG_CODE = 5000;


    public static JsonResult Get() {
        return new JsonResult();
    }

    public static JsonResult Get(boolean success) {
        return new JsonResult(success);
    }

    public static JsonResult Get(boolean success, String msg) {
        return new JsonResult(success, msg);
    }

    public static JsonResult Get(boolean success, String msg, Integer code) {
        return new JsonResult(success, msg, code);
    }

    public static JsonResult Get(String key, Object value) {
        return (new JsonResult()).put(key, value);
    }

    public JsonResult() {
        this(true);
    }

    public JsonResult(boolean success) {
        this(success, "");
    }

    public JsonResult(boolean success, String msg) {
        this(success, msg, null);
    }

    public JsonResult(boolean success, String msg, Integer code) {
        this.set(success, msg, code);
    }

    public JsonResult set(boolean success, String msg) {
        this.setSuccess(success);
        this.setMsg(msg);
        this.setCode(null);
        return this;
    }

    public JsonResult set(boolean success, String msg, Integer code) {
        this.setSuccess(success);
        this.setMsg(msg);
        this.setCode(code);
        return this;
    }

    public boolean isSuccess() {
        return (Boolean) super.get("success");
    }

    public JsonResult setSuccess(boolean success) {
        super.put("success", success);
        return this;
    }

    public Boolean getSuccess() {
        return (Boolean) super.get("success");
    }

    public String getMsg() {
        return (String) super.get("msg");
    }

    public JsonResult setMsg(String msg) {
        super.put("msg", msg);
        return this;
    }

    public Integer getCode() {
        return (Integer) super.get("code");
    }

    public JsonResult setCode(Integer code) {
        super.put("code", code);
        return this;
    }

    @Override
    public JsonResult put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public JsonResult putRes(Object value) {
        super.put("res", value);
        return this;
    }

    public JsonResult putList(List<?> list) {
        super.put("list", list);
        return this;
    }

    public JsonResult putPage(Object value) {
        super.put("page", value);
        return this;
    }
}
